package com.yj.springbootparams_analysis.config;

import com.yj.springbootparams_analysis.req.CommentReq;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;

public class HandlerArgumentResolver implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {

        if (methodParameter.getParameterType().isAssignableFrom(CommentReq.class)){
            return true;
        }
        return false;
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {

        CommentReq commentReq = new CommentReq();

        HttpServletRequest request = (HttpServletRequest) nativeWebRequest.getNativeRequest();
        String current = request.getParameter("current");
        String limit = request.getParameter("limit");

        System.out.println(current);
        System.out.println(limit);

        commentReq.setLimit(Integer.parseInt(limit));
        commentReq.setCurrent(Integer.parseInt(current));

        return commentReq;
    }
}
